@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!-- 引入 layui.css -->
<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form layui-form-pane" action="">
    <div style="padding: 20px;">
        <h3><i class="layui-icon layui-icon-add-1"></i> 新增工序分类</h3>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">工序分类编号</label>
            <div class="layui-input-block">
                <input type="text" name="processTypeCode" id="processTypeCode" autocomplete="off" class="layui-input" readonly>
            </div>
            <div class="layui-input-inline" style="width: 80px;">
                <button type="button" class="layui-btn layui-btn-sm" onclick="generateProcessTypeCode()">生成编号</button>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">工序分类名称</label>
            <div class="layui-input-inline">
                <input type="text" name="processTypeName" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item" pane>
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="processTypeStatus" value="true" title="启用" checked>
            <input type="radio" name="processTypeStatus" value="false" title="禁用">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="typeRemark" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="demo2">提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<script src="~/js/site.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form;
        var layer = layui.layer;
        
        // 页面加载完成后自动生成编号
        $(document).ready(function() {
            generateProcessTypeCode();
        });
        
        // 自动生成工序分类编号
        function generateProcessTypeCode() {
            var currentDate = new Date();
            var year = currentDate.getFullYear();
            var month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
            var day = currentDate.getDate().toString().padStart(2, '0');
            var sequence = (Math.floor(Math.random() * 9000) + 1000).toString(); // 生成4位随机序号
            var processTypeCode = 'T' + year + month + day + sequence;
            $('#processTypeCode').val(processTypeCode);
        }
        
        // 提交事件
        form.on('submit(demo2)', function (data) {
            var field = data.field; // 获取表单字段值

            var $submitBtn = $('button[lay-submit]');

            // 防止重复提交
            if ($submitBtn.prop('disabled')) {
                return false;
            }

            // 禁用提交按钮
            $submitBtn.prop('disabled', true).text('提交中...');

            // 将表单数据包装成API期望的格式
            var request = {
                processTypeCode: field.processTypeCode.trim(),
                processTypeName: field.processTypeName.trim(),
                processTypeStatus: field.processTypeStatus === 'true', // 转换为布尔值
                typeRemark: field.typeRemark ? field.typeRemark.trim() : null
            };

            // 此处可执行 Ajax 等操作
            $.post({
                url: 'http://localhost:5141/api/Process/AddProcessType',
                data: JSON.stringify(request),
                contentType: 'application/json',
                success: function (res) {
                    if (res > 0) {
                        alert('添加成功');
                        // 清空表单
                        $('form')[0].reset();
                        form.render();
                         if (window.parent && window.parent.loadPage) {
                                window.parent.loadPage('/Process/ProcessTypeList', '工序分类管理列表');
                         }
                    }
                    else {
                        alert('添加失败');
                    }
                }
            });
            return false; // 阻止默认 form 跳转
        });
    });
</script>

